function getQueryString(name) {
	var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
	var r = window.location.search.substr(1).match(reg);
	if(r != null) {
		return decodeURI(r[2]);
	}
	return null;
}

var roleId = '';
var roleType = 0;
var flag = 0;
var gourl = 'role.html';

var menuIds = [];
$('#get_selected_vals').on("click", function() {
	alert();
});
//初始化赋值，判断是新增还是编辑
window.onload = function() {
	var roleId1 = GetQueryString("roleId1");
	var remark1 = getQueryString("remark1");
	var roleName1 = getQueryString("roleName1");
	var roleType1 = getQueryString("roleType1");

	var param2 = "/sys/getAllMenuList";
	roleType = roleType1;
	roleId = roleId1;
	if(roleType == 0) {
		$('.roleTypeName').text("员工角色");
		param2 = "/sys/getStaffMenuList";
	} else {
		$('.roleTypeName').text("广告主角色");
		param2 = "/sys/getAdUserMenuList";
		gourl = 'role2.html';
	}
	//	roleId1存在为编辑页面
	if(roleId1) {
		flag = 1;
		$('.rolename').val(roleName1);
		$('.roledescription').val(remark1);
	}

	var method2 = "post";
	getAgentMsg2(param2, method2);
//	console.log(dataResult2);
	if(dataResult2.data) {
		var treeResult = [];

		for(var j = 0; j < dataResult2.data.length; j++) {
			var d1 = {};
			d1.name = dataResult2.data[j].name;
			d1.inputName = dataResult2.data[j].name;
			d1.value = dataResult2.data[j].menuId;
			if(dataResult2.data[j].child) {// && dataResult2.data[j].child.length != 0

				d1.children = [];
				for(var i = 0; i < dataResult2.data[j].child.length; i++) {
					var d2 = {};
					d2.name = dataResult2.data[j].child[i].name;
					d2.inputName = dataResult2.data[j].child[i].name;
					d2.value = dataResult2.data[j].child[i].menuId;

					if(  dataResult2.data[j].child[i].child) {
						d2.children = [];
						for(var k = 0; k < dataResult2.data[j].child[i].child.length; k++) {
							var d3 = {};
							d3.name = dataResult2.data[j].child[i].child[k].name;
							d3.inputName = dataResult2.data[j].child[i].child[k].name;
							d3.value = dataResult2.data[j].child[i].child[k].menuId;

							if(dataResult2.data[j].child[i].child[k].child) {
								d3.child = [];

								//							for(var u=0;u<dataResult2.data[j].child[i].child[k].child.length;u++){
								//								var d4 = {};
								//								d4.name = dataResult2.data[j].child[i].child[k].child[u].name;
								//								d4.inputName = dataResult2.data[j].child[i].child[k].child[u].name;
								//								d4.value = dataResult2.data[j].child[i].child[k].child[u].menuId;
								//								if(dataResult2.data[j].child[i].child[k].child.length != 0){
								//									d4.child = [];
								//								}
								//								d3.children.push(d4);
								//							}
							}else{
							//第三个循环的else
								dataResult2.data[j].child[i].child[k].child = [];
							}
							d2.children.push(d3);
						}
					}else{
					//第二个循环的else
						dataResult2.data[j].child[i].child = [];
					}
					d1.children.push(d2);
				}

			}else{
			//第一个循环的else
				dataResult2.data[j].child = [];
			}
			treeResult.push(d1);
		}
//			console.log(treeResult);

		var treeData = {
			// 复选框change事件
			onchange: function(input, yntree) {
				//				console.log(this);
			},
			// 是否严格的遵循父子互相关联的做法
			checkStrictly: true,
			data: treeResult
		};

		var yntree = new YnTree(document.getElementById("tree"), treeData);

		//控制默认展开
		if(yntree.data) {
			for(var i = 0; i < yntree.data.length; i++) {
				yntree.data[i].spread(true);
				if(yntree.data[i].children) {
					for(var k = 0; k < yntree.data[i].children.length; k++) {
						yntree.data[i].children[k].spread(true);
					}
				}

			}
		}
		

	}

	//新增或编辑角色按钮
	$(".addPer").on("click", function() {
		$(this).attr("disabled", true).css("background", "#BBBBBB");
		method = "post";
		if(yntree.getValues()){
			menuIds = yntree.getValues();
//			console.log(menuIds);	
		}
		

		//		var menuIds = [];
		var remark = $('.roledescription').val();
		var roleName = $('.rolename').val();

		if(flag == 0) { //新增
			datas = { menuIds: menuIds, remark: remark, roleName: roleName, roleType: roleType };
			param = "/sys/saveRole";

		} else {
			param = "/sys/updateRole";
			datas = { menuIds: menuIds, remark: remark, roleName: roleName, roleType: roleType, roleId: roleId };

		}

		//	if(!menuIds || !remark || !roleName || !roleType){
		//		layer.msg("用户信息不能为空！！");
		//		$(this).attr("disabled",false).css("background","#4A9DD2");
		//		return false;
		//	}
		getAgentMsg(param, method, datas);
		

		layer.msg(dataResult.msg,{time:2*1000},function() {
			window.location.href = gourl + token_str;
		});

	});
	document.getElementById("reInit_tree").onclick = function() {
		yntree.reInit(treeData.data);
//		console.log(yntree);
	}

}